package com.ruoyi.web.controller.mes;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.mes.MesCustom;
import com.ruoyi.web.service.IMesMachineCaredetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.mes.MesMachineCare;
import com.ruoyi.web.service.IMesMachineCareService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备保养计划Controller
 *
 * @author levicash
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/mes/care")
public class MesMachineCareController extends BaseController {
    @Autowired
    private IMesMachineCareService mesMachineCareService;

    @Autowired
    private IMesMachineCaredetailService mesMachineCaredetailService;

    /**
     * 查询设备保养计划列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesMachineCare mesMachineCare) {
        startPage();
        List<MesMachineCare> list = mesMachineCareService.selectMesMachineCareList(mesMachineCare);
        return getDataTable(list);
    }

    /**
     * 导出设备保养计划列表
     */
    @Log(title = "设备保养计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesMachineCare mesMachineCare) {
        List<MesMachineCare> list = mesMachineCareService.selectMesMachineCareList(mesMachineCare);
        ExcelUtil<MesMachineCare> util = new ExcelUtil<MesMachineCare>(MesMachineCare.class);
        util.exportExcel(response, list, "设备保养计划数据");
    }

    /**
     * 获取设备保养计划详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesMachineCareService.selectMesMachineCareById(id));
    }

    /**
     * 新增设备保养计划
     */
    @Log(title = "设备保养计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesMachineCare mesMachineCare) {
//        boolean isExist = mesMachineCareService.checkDeviceCareUnique(mesMachineCare.getCode());
//        if (isExist) {
//            return AjaxResult.warn("新增保养计划'" + mesMachineCare.getCode() + "'失败，计划编号已存在");
//        }
        Integer response = mesMachineCareService.insertMesMachineCare(mesMachineCare);
        if (response.equals(0)) {
            return AjaxResult.warn("新增保养计划'" + mesMachineCare.getCode() + "'失败，请检查时间设置");
        }
        return toAjax(response);
    }

    /**
     * 修改设备保养计划
     */
    @Log(title = "设备保养计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesMachineCare mesMachineCare) {
        return toAjax(mesMachineCareService.updateMesMachineCare(mesMachineCare));
    }

    /**
     * 删除设备保养计划
     */
    @Log(title = "设备保养计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mesMachineCareService.deleteMesMachineCareByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "设备保养计划", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MesMachineCare mesMachineCare) {
        mesMachineCare.setUpdateBy(getUsername());
        return toAjax(mesMachineCareService.updateMesMachineCareStatus(mesMachineCare));
    }

    /**
     * 根据保养计划生成保养记录（手动点生成）
     */
    @Log(title = "设备保养计划", businessType = BusinessType.INSERT)
    @PostMapping("/addDetail")
    public AjaxResult addDetail(@RequestBody MesMachineCare mesMachineCare) {
        Integer response = mesMachineCareService.AddDetail(mesMachineCare);
        if (response <= 0) {
            return AjaxResult.warn("新增保养记录失败'");
        }
        return toAjax(response);
    }
}
